package com.top.mall.main.home

import android.graphics.Color
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BarUtils
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.app.SPSConstance
import com.top.mall.jetpack.vm.MainViewModel
import com.top.mall.main.access.AccessActivity
import com.top.mall.main.cart.CartFragment
import com.top.mall.main.home.adapter.StatePagerAdapter
import com.top.mall.main.income.IncomeFragment
import com.top.mall.main.mine.MineFragment
import com.top.mall.main.order.OrderFragment
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : OwnerViewModeActivity<MainViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(MainActivity::class.java)
        }
    }

    private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() {
        override fun onFragmentResumed(fm: FragmentManager, fragment: Fragment) {
            super.onFragmentResumed(fm, fragment)
            when (fragment) {
                is HomeFragment -> {
                    BarUtils.setStatusBarLightMode(
                        fragment.requireActivity(), (fragment.alphaStatus > 180)
                    )
                    val color: Int = Color.argb(fragment.alphaStatus, (255), (255), (255))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), color)
                }
                is IncomeFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (true))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), Color.WHITE)
                }
                is CartFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (true))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), Color.WHITE)
                }
                is OrderFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (true))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), Color.WHITE)
                }
                is MineFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (false))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), (0x00000000))
                }
            }
        }
    }

    override fun viewModel(): MainViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_main


    override fun initView() {

        if (SPSConstance.token.isNullOrEmpty()) {
            AccessActivity.open()
            finish()
        }

        val mainFragments: MutableList<Fragment> = mutableListOf(
            HomeFragment.newInstance(),
            IncomeFragment.newInstance(),
            CartFragment.newInstance(),
            OrderFragment.newInstance(),
            MineFragment.newInstance()
        )
        val mainStatePager = StatePagerAdapter(
            supportFragmentManager,
            FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,
            mainFragments
        )
        view_pager_main.offscreenPageLimit = mainFragments.size
        view_pager_main.setScrollAnim(false)
        view_pager_main.adapter = mainStatePager

        nav_view_main.setOnNavigationItemSelectedListener { item ->
            val position = when (item.itemId) {
                R.id.item_main_income -> 1
                R.id.item_main_cart -> 2
                R.id.item_main_order -> 3
                R.id.item_main_mine -> 4
                else -> 0
            }
            view_pager_main.currentItem = position
            return@setOnNavigationItemSelectedListener true
        }

        supportFragmentManager.registerFragmentLifecycleCallbacks(
            fragmentLifecycleCallbacks,
            (true)
        )

    }

    override fun onDestroy() {
        super.onDestroy()
        supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks)
    }
}